import { Node } from "cc";
import { GameMgr } from "../GameMgr";
import { Food } from "./Food";

/**
 * 食物管理器
 */
export class FoodMgr {

    foodMap = new Map<number, Food>();

    constructor(
        /** 游戏层级 */
        public readonly layer: Node
    ) { }

    addFood(data: kp_entity_food) {
        const food = Food.get().onInit(data);
        this.foodMap.set(data.id, food);
        this.layer.addChild(food.node);
    }

    removeFood(id: number) {
        const food = this.foodMap.get(id);
        if (food) {
            food.onRemove();
            this.foodMap.delete(id);
        }
    }

    onFoodEaten(data: kp_entity_eatenFood) {
        const food = this.foodMap.get(data.id);
        if (food) {
            const eater = GameMgr.ins.snakeMgr.bodyMap.get(data.eaterId);
            food.onRemove(eater);
            this.foodMap.delete(data.id);
        }
    }

}